package com.audaque.springboot.foshanupload.web.reactivedemo.client;



import com.audaque.springboot.foshanupload.web.reactivedemo.processer.Processer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

/**
 * create by 尼恩 @ 疯狂创客圈
 **/
@Slf4j
@Component
public class EchoClient {

    public void start() throws IOException {

        InetSocketAddress address =
                new InetSocketAddress("localhost", 9501);

        // 1、获取通道（channel）
        SocketChannel socketChannel = SocketChannel.open(address);
        log.debug("客户端连接成功");
        // 2、切换成非阻塞模式
        socketChannel.configureBlocking(false);
        //不断的自旋、等待连接完成，或者做一些其他的事情
        while (!socketChannel.finishConnect()) {

        }
        log.debug("客户端启动成功！");

        //启动接受线程
        Processer processer = new Processer(socketChannel);
        new Thread(processer).start();

    }



}
